<template>
  <a-card :bordered="false" style="height: 90%">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="名称">
              <a-input v-model="queryParam.name" placeholder="名称"/>
            </a-form-item>
          </a-col>
          <a-col :md="!advanced && 8 || 24" :sm="24">
            <span
              class="table-page-search-submitButtons"
              :style="advanced && { float: 'right', overflow: 'hidden' } || {} "
            >
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button v-if="$auth('sys_role_add')" type="primary" icon="plus" @click="handleEdit()">新建</a-button>
    </div>

    <s-table
      ref="table"
      size="middle"
      :row-key="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :page-size="10"
      :pagination="pagination"
    >
      <span slot="action" slot-scope="text, record">
        <template>
          <div>
            <a-button v-if="$auth('sys_role_edit')" type="primary" @click="handleEdit(record)">
              编辑
            </a-button>
            <a-button
              v-if="$auth('sys_role_del')"
              style="margin-left: 8px"
              type="danger"
              @click="deleteRow(record.id)"
            >
              删除
            </a-button>
          </div>
        </template>
      </span>
    </s-table>

    <!--编辑-->
    <role-edit ref="roleEdit" @ok="$refs.table.refresh(true)"/>
  </a-card>
</template>
<style scoped lang="scss">
  .table-operator {
    margin-bottom: 10px;
  }
</style>
<script>
  import moment from 'moment'
  import {STable} from '@/components'
  import RoleEdit from './Edit'
  import {getSysRoleList} from '@/api/manage'
  import {deleteRole} from '@/api/role'

  export default {
    name: 'Index',
    components: {
      STable,
      RoleEdit
    },
    data() {
      return {
        mdl: {},
        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '名称',
            dataIndex: 'name'
          },
          {
            title: '编码',
            dataIndex: 'code'
          },
          {
            title: '备注',
            dataIndex: 'remark'
          },
          {
            title: '操作',
            dataIndex: 'action',
            scopedSlots: {customRender: 'action'}
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          // eslint-disable-next-line no-undef
          return getSysRoleList(Object.assign(parameter, this.queryParam))
            .then(res => {
              return res.data
            })
        },
        selectedRowKeys: [],
        selectedRows: [],
        // custom table alert & rowSelection
        options: {
          alert: {
            show: true, clear: () => {
              this.selectedRowKeys = []
            }
          },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange
          }
        },
        pagination: {
          total: 0,
          pageSize: 10, // 每页中显示10条数据
          showSizeChanger: true,
          pageSizeOptions: ['10', '20', '50', '100'], // 每页中显示的数据
          showTotal: total => `共有 ${total} 条数据` // 分页中显示总的数据
        }
      }
    },
    created() {
    },
    methods: {
      handleEdit(obj) {
        var form = Object.assign({}, obj)
        this.$refs.roleEdit.initData(form)
      },
      deleteRow(id) {
        const _this = this
        // eslint-disable-next-line no-unused-vars
        _this.$confirm({
          title: '确认删除该角色吗?',
          content: '删除后将不可恢复',
          okText: '确定',
          okType: 'danger',
          cancelText: '取消',
          onOk() {
            deleteRole(id).then(res => {
              if (res.code === '0') {
                _this.$message.success('删除成功')
                _this.$refs.table.refresh(true)
              } else {
                _this.$message.error(res.message)
              }
            })
          },
          onCancel() {
          }
        })
      },
      onSelectChange: function (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },

      resetSearchForm() {
        this.queryParam = {
          date: moment(new Date())
        }
      }
    }
  }
</script>
